# ------------------------------------------------------------------------------------------------
### Generate Table 2: Effects on overall, opposition, and government turnout
# ------------------------------------------------------------------------------------------------

# Load data 
load("../data/results/sdid_results.Rda")

# Filter and prepare data
filtered_results <- filter_sdid_results(
  sdid_results,
  outcome_filter = c("Overall Turnout", "Party Turnout"),
  party_filter = c("Opposition", "Overall Turnout", "Law and Justice (PiS)")
) %>%
  mutate(
    outcome = case_when(
      outcome == "Party Turnout" & party == "Opposition" ~ "Opposition Turnout",
      outcome == "Party Turnout" & party == "Law and Justice (PiS)" ~ "Government Turnout",
      TRUE ~ outcome
    ),
    outcome = factor(outcome, levels = c("Overall Turnout", "Opposition Turnout", "Government Turnout")),
    sample = ifelse(sample == "< 50km Sample", "$<$ 50km Sample", sample),
    sample = factor(sample, levels = c("$<$ 50km Sample", "Full Sample"))
  ) %>%
  arrange(outcome)

# Generate and save table
latex_table <- create_latex_table(
  filtered_results, 
  group_var = "outcome", 
  row_var = "sample",
  caption = "Synthetic Difference-in-Differences Estimates for Turnout",
  label = "tab:sdid_turnout",
  col_names = c("Outcome", "Sample", "Estimate (SE)", "N")
)

save_table(latex_table, "../output/tables/table2.txt")
